package com.chatplus.application.web.basecontroller;

import com.chatplus.application.common.enumeration.PhoneNumberSection;
import com.chatplus.application.common.exception.BadRequestException;
import com.chatplus.application.common.util.CommonUtils;
import com.chatplus.application.web.WebAutoConfiguration;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("devops")
@ConditionalOnProperty(value = "enableDevOpsController", prefix = WebAutoConfiguration.CONFIG_KEY_ROOT, matchIfMissing = true)
public class DevOpsController {
    @PostMapping("detectPhoneNumberSection")
    public PhoneNumberSection detectPhoneNumberSection(String phone) {
        PhoneNumberSection section = CommonUtils.detectPhoneNumberSection(phone);
        if (null == section) throw new BadRequestException("无效号码", phone);

        return section;
    }

    @RequestMapping("context")
    public Map<String, Object> context(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();

        map.put("Scheme", request.getScheme());
        map.put("Host/ServerName", request.getServerName());
        map.put("Port", request.getServerPort());
        map.put("ContextPath", request.getContextPath());
        map.put("RequestURI", request.getRequestURI());
        map.put("RequestURL", request.getRequestURL());
        map.put("RemoteAddr", request.getRemoteAddr());

        return map;
    }
}
